/**
 * https://leetcode.cn/problems/fruit-into-baskets/description/
 * 904. 水果成篮
 * medium, 赖德檀 2024.09.04
 * 滑动窗口
 */
 class Solution {
public:
    int totalFruit(vector<int>& fruits) {
        int sum=0;
        unordered_map<int,int>mp;
        int l,r;
        for(l=0,r=0;r<fruits.size();r++)
        {
            if(mp.find(fruits[r])!=mp.end())
            mp[fruits[r]]++;
            else if(mp.size()<2)
                mp[fruits[r]]++;
             else
             {
                sum=max(sum,r-l);
               do
                {
                    mp[fruits[l]]--;
                }while(mp[fruits[l++]]>0);
                mp[fruits[r]]++;
                mp.erase(fruits[l-1]);
             }
        } 
    sum=max(sum,r-l);      
    return sum;
    }
};
